package org.world.infrastructure.persistent.persistent.impl;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.stereotype.Component;
import org.world.domain.aggregate.sheep.SheepRepository;
import org.world.domain.aggregate.sheep.entity.Sheep;
import org.world.infrastructure.dao.domain.SheepDao;

import javax.annotation.Resource;

@Component
public class SheepMyBatisRepositoryImpl implements SheepRepository {
    @Resource
    private SheepDao dao;

    @Override
    public Sheep load(Long id) {
        return dao.selectById(id);
    }

    @Override
    public Long save(Sheep entity) {
        if (!ObjectUtil.isNull(entity.getId()) && !ObjectUtil.isNull(dao.selectById(entity.getId()))) {
            dao.updateById(entity);
        } else {
            dao.insert(entity);
        }

        return entity.getId();
    }

    @Override
    public void delete(Long id) {
        dao.deleteById(id);
    }
}
